<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 图像区域 -->
            <div class="avatar_box">
                <img src="../assets/image/logo.jpg" alt="">
            </div>
            <!-- 登录表单区域 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
                <!-- 用户名区域 -->
                <el-form-item prop="username">
                  <el-input v-model="loginForm.username" prefix-icon="el-icon-loading"></el-input>
                </el-form-item>
                <!-- 密码区域 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-loading" type="password"></el-input>
                  </el-form-item>
                  <!-- 按钮区域 -->
                  <el-form-item>
                    <el-form-item class="btn_item">
                        <el-button @click="login" type="primary">登录</el-button>
                        <el-button @click="resetLoginForm" type="primary">重置</el-button>
                      </el-form-item>
                  </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import axios from 'axios';
    export default {
        data() {

            return {
                // 登录表单的数据绑定对象
                loginForm: {
                    username: '',
                    password: ''
                },
                // 表单验证的规则
                loginFormRules: {
                    // 验证用户名是否合法
                    username: [{
                        required: true,
                        message: '请输入登录账户',
                        trigger: 'blur'
                    }, {
                        min: 5,
                        max: 15,
                        message: '长度在 5 到 15 个字符之间',
                        trigger: 'blur'
                    }],
                    // 验证密码是否合法
                    password: [{
                        required: true,
                        message: '请输入账户密码',
                        trigger: 'blur'
                    }, {
                        min: 6,
                        max: 10,
                        message: '长度在 6 到 10 个字符之间',
                        trigger: 'blur'
                    }]

                }
            };
        },
        methods: {
            resetLoginForm() {
                //对表单进行重置操作
                this.$refs.loginFormRef.resetFields();
            },
            login() {
                var that = this;
                // 登录前进行表单验证，是否通过
                this.$refs.loginFormRef.validate(valid => {
                    // 发送登录请求
                    if (!valid) return;
                    axios
                        .post('systemv1/login/button/', {
                            'username': this.loginForm.username,
                            'password': this.loginForm.password
                        })
                        .then(function(res) {
                            // 请求成功后
                            if (res.data.meta.status == 200) {
                                that.$message.success('登录成功！');
                                console.log('登录成功！');
                                // 将token保存客户端的ssessionstorage中
                                window.sessionStorage.setItem('token', res.data.data.token);
                                // 跳转到主页面
                                that.$router.push('/home');

                            } else {
                                console.log('登录失败！');
                                return that.$message.error('用户名或密码有误！');
                            };


                        })
                        .catch(function(err) {
                            // 请求失败
                            console.log(err);
                        });
                })
            }
        }
    };
</script>


<style lang="less" scoped>
    .login_container {
        background-color: #409EFF;
        height: 100%;
    }
    
    .login_box {
        width: 550px;
        height: 350px;
        background-color: #fff;
        border-radius: 5px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    
    .avatar_box {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        position: absolute;
        left: 50%;
        transform: translate(-50%, 0);
    }
    
    .avatar_box img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
    }
    
    .btn_item {
        display: flex;
        justify-content: center;
    }
    
    .login_form {
        position: absolute;
        bottom: 5%;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
</style>>